/**
 * Created by ljg on 2017/2/22.
 */
import './style.css';

const uiTree=function () {
    angular.module('ui.tree',['ui.help']);
    angular.module('ui.tree')
        .directive('uiTree', function (eventHelper) {
            return {
                restrict: 'E',
                replace: true,
                controller:function ($scope,$attrs) {
                    const idName=$attrs.idName||'code';
                    const pidName=$attrs.pidName||'parentCode';
                    const editable=$attrs.editable?true:false;

                    const setting = {
                        edit: {
                            enable: editable,
                            removeTitle:'删除',
                            renameTitle:'编辑'

                        },
                        view: {
                            dblClickExpand: false,
                            showLine: true,
                            selectedMulti: false,
                            fontCss: function(treeId, treeNode) {
                                        return treeNode.selected == true ? {color:"red"} : {};
                                    }
                        },
                        data: {
                            simpleData: {
                                enable:true,
                                idKey: idName,
                                pIdKey: pidName,
                                rootPId: ""
                            },
                            key:{
                                checked:'checked',
                                url: "xUrl"

                            },
                        },
                        check: {
                            enable: $attrs.check,
                            chkboxType: { "Y": "ps", "N": "s" }

                        },
                        callback: {
                            beforeEditName: $scope.$parent.editTree,
                            beforeRemove: $scope.$parent.removeTree

                        }
                    };
                    if(editable){
                        setting['view']['addHoverDom']=function(treeId, treeNode) {
                            var sObj = $("#" + treeNode.tId + "_span");
                            if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
                            var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
                                + "' title='新增' onfocus='this.blur();'></span>";
                            sObj.after(addStr);
                            var btn = $("#addBtn_"+treeNode.tId);
                            if (btn) btn.bind("click", function(){
                                $scope.$parent.addTree(treeId,treeNode);
                                return false;
                            });
                        };
                        setting['view']['removeHoverDom']=function (treeId, treeNode) {
                            $("#addBtn_"+treeNode.tId).unbind().remove();
                        };
                    }
                    const treeKey=$attrs.key||'_tree';
                    const expand=$attrs.expand||'1';
                    const _this=this;
                    this._queryData=function () {
                        $.ajax({
                            url:$attrs.url,
                            type:$attrs.method||'post'
                        }).done(function (result) {
                            if(result.code!='0000'){
                                toastr.error("获取树数据失败,"+result.msg);
                                return;
                            }
                            const zNodes=result.data||[];
                            const idName=$attrs.idName||'code';
                            const root={};
                            root[idName]=null;
                            root['name']='根目录';
                            zNodes.push(root);

                            const ztree = $.fn.zTree.init($("#"+treeKey),setting, zNodes);
                            ztree.expandAll(false);

                            const nodes=ztree.getNodesByParam('level',expand)||[];
                            for (let i in nodes){
                                ztree.expandNode(nodes[i],true,false);
                            }
                            let selectedNode=ztree.getNodeByParam('selected',true);
                            if(selectedNode){
                                let parentNode=selectedNode.getParentNode();
                                while (parentNode!=null){
                                    ztree.expandNode(parentNode,true,false);
                                    parentNode=parentNode.getParentNode();
                                }
                            }
                            const key=$attrs.key||'$tree';
                            $scope.$parent[key]=ztree;
                            $scope.$parent[key]['_queryData']=_this._queryData;
                        }).fail(function () {
                            toastr.error("获取树数据失败");
                        });
                    }
                    this._queryData();

                },
                scope:{},
                template: function(el,attr){
                    const treeKey=attr.key||'_tree';

                    return  '<ul id='+treeKey+'  class="ztree" style="width:260px; overflow:auto;"></ul>';
                },
                link : function(scope, element, attrs) {
                }
            };
        });
}
export default uiTree;